package com.yjt.fresh.fragment;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.http.HttpManager;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.listener.HttpOnNextListener;
import com.yjt.fresh.R;
import com.yjt.fresh.adpater.AdpRvAtyProductDetailComment;
import com.yjt.fresh.api.ApiGetProductComment;
import com.yjt.fresh.base.BaseSimpleFragment;
import com.yjt.fresh.bean.BeanHomePage;
import com.yjt.fresh.bean.BeanMyPoints;
import com.yjt.fresh.bean.BeanProductDetail;
import com.yjt.fresh.constant.MyConstants;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 商品详情的商品
 * Created by mcc on 2018/6/15.
 */

public class FrgProductDetailComment extends BaseSimpleFragment {
    @BindView(R.id.rv_app_atyproductdetail_frg_comment)
    RecyclerView mRv;
    @BindView(R.id.srlAppFrgComment)
    SwipeRefreshLayout mRefreshLayout;
    private AdpRvAtyProductDetailComment mAdpComment;
    private int mNextRequestPage = 1;
    private boolean isLoadMore;

    @Override
    protected int getContentView() {
        return R.layout.app_atyproductdetail_frg_comment;
    }

    @Override
    protected void initView(Bundle savedInstanceState, View view) {
        ButterKnife.bind(this, view);
    }

    @Override
    protected void initEvents() {
        initRv();
        getData();
        iniRefresh();
    }

    private void iniRefresh() {
        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mNextRequestPage = 1;
                isLoadMore = false;
                getData();
            }
        });

        mAdpComment.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                isLoadMore = true;
                getData();
            }
        }, mRv);
    }

    HttpOnNextListener<List<BeanProductDetail.EVALUATELISTBean>> mListener = new HttpOnNextListener<List<BeanProductDetail.EVALUATELISTBean>>() {
        @Override
        public void onNext(List<BeanProductDetail.EVALUATELISTBean> evaluatelistBeans) {
            mAdpComment.setEmptyView(R.layout.view_empty);
            if (isLoadMore) {
                setData(false, evaluatelistBeans);
            } else {
                setData(true, evaluatelistBeans);
                mRefreshLayout.setRefreshing(false);
            }
        }

        @Override
        public void onReLogin() {
            toLogin();
        }
    };

    private void setData(boolean isRefresh, List<BeanProductDetail.EVALUATELISTBean> data) {
        mNextRequestPage++;
        final int size = data == null ? 0 : data.size();
        if (isRefresh) {
            mAdpComment.setNewData(data);
        } else {
            if (size > 0) {
                mAdpComment.addData(data);
                mAdpComment.loadMoreComplete();
            }else {
                mAdpComment.loadMoreEnd(true);
            }
        }

        if (size < 20){
            mAdpComment.loadMoreEnd(true);
        }
    }
    private void getData() {
        ApiGetProductComment apiGetProductComment = new ApiGetProductComment(mListener, mContext, true, false, false);
        Map<String, Object> map = new HashMap<>();
        map.put("currentPage", mNextRequestPage);
        map.put("GOODS_ID", getArguments().getString(MyConstants.GOODS_ID));
        apiGetProductComment.setMap(map);
        HttpManager.getInstance().doHttpDeal(apiGetProductComment);
    }

    private void initRv() {
        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        mRv.setLayoutManager(manager);
        List<BeanProductDetail.EVALUATELISTBean> list = new ArrayList<>();
        mAdpComment = new AdpRvAtyProductDetailComment(R.layout.app_recycle_item_comment, list);
        mRv.setAdapter(mAdpComment);
    }

}
